package com.koron.common.core.business.message.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.message.bean.po.MessageHistoryPo;
import com.koron.common.core.business.message.bean.po.MessageTopicPo;
import com.koron.common.core.business.message.bean.query.MessageHistoryQuery;
import com.koron.common.core.business.message.mapper.MessageHistoryMapper;
import com.koron.common.core.business.message.service.MessageHistoryService;
import com.koron.common.core.business.message.service.MessageTopicService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 消息发送历史表(MessageHistory)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-16 14:13:21
 */
@Service("messageHistoryService")
public class MessageHistoryServiceImpl implements MessageHistoryService {

    @Autowired
    MessageTopicService messageTopicService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, MessageHistoryPo messageHistory) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        return mapper.insertOrUpdate(messageHistory);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<MessageHistoryPo> messageHistoryList) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        return mapper.insertOrUpdateBatch(messageHistoryList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MessageHistoryPo messageHistory) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        return mapper.insert(messageHistory);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MessageHistoryPo> messageHistoryList) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        return mapper.insertBatch(messageHistoryList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MessageHistoryPo messageHistory) {
        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);
        return mapper.update(messageHistory);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public MessageHistoryPo queryById(SessionFactory factory, String id) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MessageHistoryPo> queryList(SessionFactory factory, MessageHistoryQuery messageHistoryQuery) {

        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        List<MessageHistoryPo> list = mapper.queryList(messageHistoryQuery);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, MessageHistoryQuery messageHistoryQuery, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(false));
        MessageHistoryMapper mapper = factory.getMapper(MessageHistoryMapper.class);

        List<MessageHistoryPo> list = mapper.queryList(messageHistoryQuery);

        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo(list);
        }

        List<MessageTopicPo> messageTopicPos = ADOConnection.runTask(messageTopicService, "queryList", List.class, new MessageTopicPo());

        HashMap<String, String> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(messageTopicPos)) {
            messageTopicPos.forEach(p -> {
                map.put(p.getTopicCode(), p.getTopicName());
            });

            list.forEach(p -> p.setTopicName(map.get(p.getTopicCode())));
        }

        return new PageInfo(list);
    }

}